package com.yyj.web.controller.manager;

import com.yyj.common.BaseResponse;
import com.yyj.constant.ReturnCode;
import com.yyj.constant.WebConstant;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.RepairRecordService;
import com.yyj.util.RspUtils;
import com.yyj.web.controller.BaseController;
import com.yyj.web.protocol.PageRequest;
import com.yyj.web.protocol.req.RepairRecordQueryReq;
import com.yyj.web.protocol.req.RepairRecordReq;
import com.yyj.web.protocol.req.RepairReplyReq;
import com.yyj.web.protocol.req.RepairStatusUpdateReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "管理员管理报修相关接口" , description="管理员管理报修相关接口")
@RestController
public class MRepairRequireController extends BaseController{

    @Autowired
    RepairRecordService repairRecordService;

    @ApiOperation(value = "管理员查询故障报修记录列表")
    @RequestMapping(value = "/auth/manager/repairs/{roomNum}" , method = RequestMethod.GET)
    public BaseResponse queryRepairRecord3(@PathVariable String roomNum , PageRequest pageRequest){
        try{
            RepairRecordQueryReq repairRecordQueryReq = new RepairRecordQueryReq();
            repairRecordQueryReq.setCurrentPage(pageRequest.getCurrentPage());
            repairRecordQueryReq.setPageSize(pageRequest.getPageSize());
            repairRecordQueryReq.setRoomNum("ALL".equals(roomNum) ? "" : roomNum);
            return RspUtils.success(repairRecordService.queryRepairRecords(repairRecordQueryReq));

        }catch(Exception e){
            logger.error("管理员查询故障记录异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }

    }

    @ApiOperation(value = "管理员查询故障报修记录详情")
    @RequestMapping(value = "/auth/manager/repairs/detail/{id}" , method = RequestMethod.GET)
    public BaseResponse queryRepairRecord2(@PathVariable long id){
        try{
            return RspUtils.success(repairRecordService.queryRepairRecordResp(id));

        }catch(Exception e){
            logger.error("管理员查询故障记录异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }

    @ApiOperation(value = "管理员更新故障报修状态")
    @RequestMapping(value = "/auth/manager/repairs/update" , method = RequestMethod.POST)
    public BaseResponse updateStatus(@RequestBody @Validated RepairStatusUpdateReq repairStatusUpdateReq , BindingResult result){
        try{
            checkRequestParams(result);
            return RspUtils.success(repairRecordService.updateStatus(repairStatusUpdateReq));

        }catch(Exception e){
            logger.error("管理员更新故障报修状态：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }

    @ApiOperation(value = "管理员回复业主故障报修问题")
    @RequestMapping(value = "/auth/manager/repairs/reply" , method = RequestMethod.POST)
    public BaseResponse reply(@RequestBody @Validated RepairReplyReq repairReplyReq , BindingResult result){
        try{
            checkRequestParams(result);
            return RspUtils.success(repairRecordService.managerReply(repairReplyReq));
        }catch(Exception e){
            logger.error("管理员回复业主故障报修问题异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }



}
